package com.jsyh.maidu.activity.me;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.BaseActivity;
import com.jsyh.maidu.adapter.orders.ScanOrderAdapter;
import com.jsyh.maidu.model.PageInfo;
import com.jsyh.maidu.model.ScanOrders;
import com.jsyh.maidu.model.ScanOrdersModel;
import com.jsyh.maidu.presenter.ScanOrdersPresenter;
import com.jsyh.maidu.views.ScanOrdersView;
import com.umeng.socialize.facebook.controller.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class ScanOrdersActivity extends BaseActivity implements ScanOrdersView, View.OnClickListener{

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView scanOrderList;
    private TextView title;
    private FrameLayout back;
    private ScanOrdersPresenter scanOrdersPresenter;
    private LinearLayoutManager linearLayoutManager;
    private PageInfo pageInfo;
    ScanOrderAdapter adapter;
    List<ScanOrders> datas;
    int mode = 0;
    int index;
    public static final int MODE_REFRESH = 1;
    public static final int MODE_ADD = 2;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_scan_orders);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.scan_orders_refresh);
        scanOrderList = (RecyclerView) findViewById(R.id.scan_orders_list);
        title = (TextView) findViewById(R.id.title);
        title.setText("扫码订单");
        back = (FrameLayout) findViewById(R.id.fl_Left);
        back.setOnClickListener(this);
        pageInfo = new PageInfo();

        scanOrdersPresenter = new ScanOrdersPresenter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        scanOrderList.setLayoutManager(linearLayoutManager);
        scanOrdersPresenter.loadScanOrdersList(this);
        refreshLayout.setColorSchemeColors(R.color.red);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(true);
                mode = MODE_REFRESH;
                scanOrdersPresenter.setPageCount(1);
                scanOrdersPresenter.loadScanOrdersList(ScanOrdersActivity.this);
            }
        });
        scanOrderList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (refreshLayout.isRefreshing()){
                    return;
                }else if (dy > 0){
                    int lastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                    int totalItem = linearLayoutManager.getItemCount();
                    index = lastItem;
                    if ((lastItem + 1) == totalItem){
                        int requestPage = scanOrdersPresenter.getPageCount() + 1;
                        if (pageInfo.getPage_limit() > 0){
                            mode = MODE_ADD;
                            double size = ((double) pageInfo.getPage_count())/pageInfo.getPage_limit();
                            if (Math.round(size) < requestPage){
                                ToastUtil.showToast(ScanOrdersActivity.this, "已到底了！");
                                return;
                            }
                            scanOrdersPresenter.setPageCount(requestPage);
                            refreshLayout.setRefreshing(true);

                            scanOrdersPresenter.loadScanOrdersList(ScanOrdersActivity.this);
                        }

                    }



                }

            }
        });

    }

    @Override
    public void getScanOrdersList(ScanOrdersModel scanOrdersModel) {
        if (refreshLayout.isRefreshing()){
            refreshLayout.setRefreshing(false);
        }
        if ("1".equals(scanOrdersModel.getCode())){
            scanOrderList.setVisibility(View.VISIBLE);
            pageInfo = scanOrdersModel.getData().getPage_info();
            if (mode == MODE_ADD){
                datas.addAll(scanOrdersModel.getData().getOrder_list());
                adapter.notifyDataSetChanged();
                linearLayoutManager.scrollToPositionWithOffset(index, 0);
                linearLayoutManager.setStackFromEnd(true);
            }else {
                datas = new ArrayList<>();
                datas = scanOrdersModel.getData().getOrder_list();
                adapter = new ScanOrderAdapter(this, datas);
                scanOrderList.setAdapter(adapter);
                scanOrderList.setItemAnimator(new DefaultItemAnimator());
                linearLayoutManager.scrollToPositionWithOffset(0, 0);
                linearLayoutManager.setStackFromEnd(true);
            }

        }else {
            scanOrderList.setVisibility(View.GONE);
            ToastUtil.showToast(this, scanOrdersModel.getMsg());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
